'''
Methods to facilitate viewing coverage data in the UCSC genome browser

Created on Sep 23, 2009

@author: mkiyer
'''

#http://genome.ucsc.edu/cgi-bin/hgTracks?org=organism_name&position=chr_position&hgt.customText=URL. 
#http://genome.ucsc.edu/cgi-bin/hgTracks?db=hg18&position=chr_position&hgt.customText=URL. 
#chr22:15916196-31832390

def get_bedgraph_track_header(name='mytrack',
                              description='mydescription'):
    '''
    track type=bedGraph name=track_label description=center_label
        visibility=display_mode color=r,g,b altColor=r,g,b
        priority=priority autoScale=on|off alwaysZero=on|off
        gridDefault=on|off maxHeightPixels=max:default:min
        graphType=bar|points viewLimits=lower:upper
        yLineMark=real-value yLineOnOff=on|off
        windowingFunction=maximum|mean|minimum smoothingWindow=off|2-16
    '''
    return ('track type=bedGraph name=%s description=%s visibility=full\n' %
            (name, description))
    
def gen_bedgraph_track_data(chrom, start, end, arr):
    '''
    http://genome.ucsc.edu/goldenPath/help/bedgraph.html

    track type=bedGraph name=track_label description=center_label
        visibility=display_mode color=r,g,b altColor=r,g,b
        priority=priority autoScale=on|off alwaysZero=on|off
        gridDefault=on|off maxHeightPixels=max:default:min
        graphType=bar|points viewLimits=lower:upper
        yLineMark=real-value yLineOnOff=on|off
        windowingFunction=maximum|mean|minimum smoothingWindow=off|2-16
  
    chromA  chromStartA  chromEndA  dataValueA
    chromB  chromStartB  chromEndB  dataValueB
    '''
    lines = []
    assert len(arr) >= (end-start)
    for pos in xrange(end - start):
        lines.append('%s\t%d\t%d\t%.2f\n' %
                     (chrom, start+pos, start+pos+1, arr[pos]))
    return lines